import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-14
 * Time: 11:53
 */
public class Demo {
    public static void main(String[] args) {
//        ExecutorService service=Executors.newCachedThreadPool();
//        //能够根据任务的数目,自动进行线程扩容
//        for (int i = 0; i < 10000; i++) {
//            int id=i;
//         service.submit(new Runnable() {
//             @Override
//             public void run() {
//                 System.out.println("hello"+id+", "+Thread.currentThread().getName());
//             }
//         });
//        }
//        ExecutorService service=Executors.newFixedThreadPool(10);
//        //创建固定数目的线程池
//        for (int i = 0; i < 10000; i++) {
//            int id=i;
//            service.submit(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("hello "+id+", "+Thread.currentThread().getName());
//                }
//            });
//        }
//        ExecutorService service=Executors.newSingleThreadExecutor();//
//       //创建一个只包含单个线程的线程池
//        for (int i = 0; i <10000 ; i++) {
//            int id=i;
//            service.submit(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("hello "+id +", "+Thread.currentThread().getName());
//                }
//            });
//        }
        //创建一个固定线程数目,但任务会延时执行的线程池
        ExecutorService service=Executors.newScheduledThreadPool(10);
        for (int i = 0; i < 10000; i++) {
            int id=i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello "+id+", "+Thread.currentThread().getName());
                }
            },10000);
        }

    }
}
